package ${basePackage}.service.impl;

import ${common}.constant.CommonEnum;
import ${common}.core.AbstractService;
import ${common}.core.PageResult;
import ${common}.util.IdGen;
import ${common}.util.ListUtil;
import ${common}.constant.CommonConstant;
import ${mapperReference};
import ${modelReference};
import ${basePackage}.service.${tableInfo.className}Service;
import ${basePackage}.vo.entity.${tableInfo.className}EntityVO;
import ${basePackage}.vo.request.${tableInfo.className}RequestVO;
import ${basePackage}.vo.response.${tableInfo.className}ResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * ${tableInfo.tableComment}
 *
 * @author ${author}
 * @date ${date}
 **/
@Slf4j
@Service
@Transactional
public class ${tableInfo.className}ServiceImpl extends AbstractService<${tableInfo.className}> implements ${tableInfo.className}Service {

    @Resource
    private ${tableInfo.className}Mapper ${tableInfo.varName}Mapper;

    /**
     * 获取${tableInfo.tableComment}分页列表
     *
     * @param ${tableInfo.varName}RequestVO ${tableInfo.tableComment}查询条件
     * @return pageResult ${tableInfo.tableComment}分页结果
     */
    @Override
    public PageResult<${tableInfo.className}ResponseVO> query${tableInfo.className}PageList(${tableInfo.className}RequestVO ${tableInfo.varName}RequestVO) {
        //
        if (${tableInfo.varName}RequestVO.getPageNumber() == null) ${tableInfo.varName}RequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (${tableInfo.varName}RequestVO.getPageSize() == null) ${tableInfo.varName}RequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<${tableInfo.className}ResponseVO> ${tableInfo.varName}ResponseVOList = Collections.emptyList();
        //
        Page<TaxSysValidConfig> page = PageHelper.startPage(${tableInfo.varName}RequestVO.getPageNumber(), ${tableInfo.varName}RequestVO.getPageSize());
        List<${tableInfo.className}> ${tableInfo.varName}List = ${tableInfo.varName}Mapper.query${tableInfo.className}List(condition);
        ${tableInfo.varName}ResponseVOList = ListUtil.convertList(${tableInfo.varName}List, ${tableInfo.className}ResponseVO.class);
        //
        // 直接从 Page 对象获取分页信息
        long total = page.getTotal();
        int pages = page.getPages();
        return PageResult.of(total, ${tableInfo.varName}ResponseVOList);
    }

    /**
     * 获取${tableInfo.tableComment}详细信息
     *
     * @param id ${tableInfo.tableComment}ID
     * @return ${tableInfo.tableComment}
     */
    @Override
    public ${tableInfo.className}EntityVO get${tableInfo.className}EntityVO(String id) {
        ${tableInfo.className} ${tableInfo.varName} = this.selectByPrimaryKey(id);
        if (null == ${tableInfo.varName}) return null;

        ${tableInfo.className}EntityVO ${tableInfo.varName}EntityVO = new ${tableInfo.className}EntityVO();
        BeanUtils.copyProperties(${tableInfo.varName}, ${tableInfo.varName}EntityVO);
        return ${tableInfo.varName}EntityVO;
    }

    /**
     * 添加${tableInfo.tableComment}
     *
     * @param ${tableInfo.varName}EntityVO ${tableInfo.tableComment}
     */
    @Override
    public void add${tableInfo.className}(${tableInfo.className}EntityVO ${tableInfo.varName}EntityVO) {
        ${tableInfo.className} ${tableInfo.varName} = new ${tableInfo.className}();
        BeanUtils.copyProperties(${tableInfo.varName}EntityVO, ${tableInfo.varName});
        ${tableInfo.varName}.setId(IdGen.next());
        this.insertSelective(${tableInfo.varName});
    }

    /**
     * 修改${tableInfo.tableComment}
     *
     * @param ${tableInfo.varName}EntityVO ${tableInfo.tableComment}
     */
    @Override
    public void update${tableInfo.className}(${tableInfo.className}EntityVO ${tableInfo.varName}EntityVO) {
        ${tableInfo.className} ${tableInfo.varName} = new ${tableInfo.className}();
        <#if varNameList?seq_contains("revision")>
        Condition condition = new Condition(${tableInfo.className}.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("id",${tableInfo.varName}EntityVO.getId());
        criteria.andEqualTo("revision",${tableInfo.varName}EntityVO.getRevision());
        this.updateByConditionSelective(${tableInfo.varName}, condition);
        <#else>
        BeanUtils.copyProperties(${tableInfo.varName}EntityVO, ${tableInfo.varName});
        this.updateByPrimaryKeySelective(${tableInfo.varName});
        </#if>
    }

    <#if varNameList?seq_contains("useable")>
    /**
     * @param id        主键
     * @param isUseable 启用状态
     */
    @Override
    public void useable${tableInfo.className}(String id, Boolean isUseable) {
        ${tableInfo.className} ${tableInfo.varName} = new ${tableInfo.className}();
        ${tableInfo.varName}.setId(id);
        Byte useable = isUseable ? CommonEnum.EnableStateEnum.ENABLE_STATE_YES.getCode() : CommonEnum.EnableStateEnum.ENABLE_STATE_NO.getCode();
        ${tableInfo.varName}.setUseable(useable);
        this.updateByPrimaryKeySelective(${tableInfo.varName});
    }
    </#if>

    /**
     * 删除${tableInfo.tableComment}
     *
     * @param id ${tableInfo.tableComment}ID
     */
    @Override
    public void delete${tableInfo.className}(String id) {
        <#if varNameList?seq_contains("deleteFlag")>
        ${tableInfo.className} ${tableInfo.varName} = new ${tableInfo.className}();
        ${tableInfo.varName}.setId(id);
        ${tableInfo.varName}.setDeleteFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(${tableInfo.varName});
        <#else>
        this.deleteByPrimaryKey(id);
        </#if>
    }
}
